package org.sdnstproject.interlayer.OF10Protocal;

import java.util.Arrays;
import java.util.Random;

import org.projectfloodlight.openflow.types.U16;
import org.projectfloodlight.openflow.types.U32;
import org.projectfloodlight.openflow.types.U8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuFlowStatsRequestV10 {
	private static final Logger log = LoggerFactory.getLogger(FuFlowStatsRequestV10.class);
    public byte version = (byte)1;
    public byte type = (byte)16;
    public short length = (short)56;//fixed value = 56;
    public long xid = 0x0L;
    public short statsType = (short)1;// fixed value property statsType = 1
    public int flags =(short) 0x1;
    public byte[] match = new byte[40];//fixed value = 40;
    public short tableId = 0x00FF;//TableId  default ALL_VAL = 0x00FF;
    public int outPort = 0xFFffFFff;//OFPort
    
	
	public FuFlowStatsRequestV10() {
		
	}
	
	public FuFlowStatsRequestV10 setXid(long xid) {
		this.xid = xid;
		return this;
	}
	
	public FuFlowStatsRequestV10 setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "statsType":
			this.statsType = Short.parseShort(value);
			break;
		case "flags":
			this.flags = Integer.parseInt(value);
			break;
		case "match":
			this.match = new byte[40];
			new Random().nextBytes(match);
			break;
		case "tableId":
			this.tableId = Short.parseShort(value);
			break;
		case "outPort":
			this.outPort = Integer.parseInt(value);
			break;
		}
		return this;
	}
	
	public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
		// fixed value property version = 1
        bb.writeByte(version);
        // fixed value property type = 16
        bb.writeByte(type);
        // fixed value property length = 56
        bb.writeShort(length);
        bb.writeInt(U32.t(xid));
        // fixed value property statsType = 1
        bb.writeShort(statsType);
        bb.writeShort(U16.t(flags));
        bb.writeBytes(match);
        bb.writeByte(U8.t(tableId));
        // pad: 1 bytes
        bb.writeZero(1);
        bb.writeShort(U16.t(outPort));
        return bb;
    }
	
	@Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFFlowStatsRequestVer10(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(",");
        b.append("statsType=").append(statsType);
        b.append(", ");
        b.append("flags=").append(flags);
        b.append(", ");
        b.append("match=").append(Arrays.toString(match));
        b.append(", ");
        b.append("tableId=").append(tableId);
        b.append(", ");
        b.append("outPort=").append(outPort);
        b.append(")");
        return b.toString();
    }
}
